package com.jobpass.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jobpass.common.insterface.GradeMapper;
import com.jobpass.common.insterface.StudentMapper;
import com.jobpass.common.model.Faculty;
import com.jobpass.common.model.Grade;
import com.jobpass.common.model.Major;
import com.jobpass.common.model.Student;
import com.jobpass.common.service.FacultyService;
import com.jobpass.common.service.GradeService;
import com.jobpass.common.service.MajorService;
import com.jobpass.common.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JobPass
 * @since 2020-07-17
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Autowired
    private FacultyService facultyService;

    @Autowired
    private MajorService majorService;

    @Autowired
    private GradeService gradeService;


    @Override
    public List<Student> FindAllStudent() {
        return studentMapper.selectList(null);
    }

    @Override
    public Student FindStudentById(Long id) {
        return studentMapper.selectById(id);
    }

    @Override
    public int DeleteStudentById(Long id) {
        return studentMapper.deleteById(id);
    }

    @Override
    public int UpdateStudentById(Student student) {
        return studentMapper.updateById(student);
    }

    @Override
    public int SavaStudent(Student student) {
        return studentMapper.insert(student);
    }

    @Override
    public Student findStudentByNum(String number) {
        QueryWrapper queryWrapper =  new QueryWrapper();
        queryWrapper.eq("number",number);
        return studentMapper.selectOne(queryWrapper);
    }


    @Override
    public int updateStudentByNum(Student student) {
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("number",student.getNumber());
        return studentMapper.update(student,updateWrapper);
    }

    @Override
    public int delStudentByNum(String number) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("number",number);
        return studentMapper.delete(queryWrapper);
    }

    @Override
    public boolean addExcelStudent(List<Student> list) {
        for (Student student : list) {
            QueryWrapper<Faculty> facultyQueryWrapper = new QueryWrapper<>();
            facultyQueryWrapper.eq("name",student.getFacultyName());
            Faculty faculty = facultyService.getOne(facultyQueryWrapper);
            if (faculty != null){
                QueryWrapper<Major> majorQueryWrapper = new QueryWrapper<>();
                majorQueryWrapper.eq("name",student.getMajorName());
                Major major = majorService.getOne(majorQueryWrapper);
                if (major != null){
                    QueryWrapper<Grade> gradeQueryWrapper = new QueryWrapper<>();
                    gradeQueryWrapper.eq("name",student.getGradeName());
                    Grade grade = gradeService.getOne(gradeQueryWrapper);
                    if (grade != null){
                        student.setGradeId(grade.getId());
                    }else {
                        Grade grade1 = new Grade();
                        grade1.setName(student.getGradeName());
                        grade1.setMajorId(major.getId());

                        boolean b1 = gradeService.save(grade1);
                        if (b1){
                            student.setGradeId(grade1.getId());
                        }
                    }
                }else{
                    Major major1 = new Major();
                    major1.setName(student.getMajorName());
                    major1.setFacultyId(faculty.getId());
                    boolean b = majorService.save(major);
                    if (b){
                        Grade grade = new Grade();
                        grade.setName(student.getGradeName());
                        grade.setMajorId(major1.getId());

                        boolean b1 = gradeService.save(grade);
                        if (b1){
                            student.setGradeId(grade.getId());
                        }
                    }
                }

            }else {
                Faculty faculty1 = new Faculty();
                faculty1.setName(student.getFacultyName());
                boolean save = facultyService.save(faculty1);
                if (save){
                    Major major = new Major();
                    major.setName(student.getMajorName());
                    major.setFacultyId(faculty1.getId());
                    boolean b = majorService.save(major);
                    if (b){
                        Grade grade = new Grade();
                        grade.setName(student.getGradeName());
                        grade.setMajorId(major.getId());

                        boolean b1 = gradeService.save(grade);
                        if (b1){
                            student.setGradeId(grade.getId());
                        }
                    }
                }
            }

        }

        boolean b = this.saveBatch(list);

        return b;
    }


}
